/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package wechat

import (
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	"log"
	//"github.com/mjiulee/lego/utils"
)

/* 根据id获取记录
 * -----------------
 */
func (t *WxCardUserCard) RecodeById(id int64) *WxCardUserCard {
	item := new(WxCardUserCard)
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}
	return item
}

func (t *WxCardUserCard) RecodeByIdWithsession(session *xorm.Session, id int64) *WxCardUserCard {
	item := new(WxCardUserCard)
	session.ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 {
		return nil
	}
	return item
}

func (t *WxCardUserCard) ReocodeByWeappIdCardIdAndCardCode(weappid string, cardid string, cardcode string) *WxCardUserCard {
	item := new(WxCardUserCard)
	//根据结构体中已有的非空数据来获得单条数据
	item.WeappId = weappid
	item.CardId = cardid
	item.UserCardCode = cardcode
	lego.GetDBEngine().Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 {
		return nil
	}
	return item
}

func (t *WxCardUserCard) ReocodeByWeappIdCardIdAndCardCodeWithsession(session *xorm.Session, weappid string, cardid string, cardcode string) *WxCardUserCard {
	item := new(WxCardUserCard)
	//根据结构体中已有的非空数据来获得单条数据
	item.WeappId = weappid
	item.CardId = cardid
	item.UserCardCode = cardcode
	session.Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 {
		return nil
	}
	return item
}

/* 添加
 * -----------------
 */
func (t *WxCardUserCard) AddRecode(item2add *WxCardUserCard) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *WxCardUserCard) AddRecodeWithsession(session *xorm.Session, item2add *WxCardUserCard) bool {
	item2add.Id = lego.UUID()

	log.Printf("WxCardUserCard.AddRecodeWithsession uuid %d, code %d", item2add.Id, item2add.UserCardCode)
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(软删除)
 * -----------------
 */
func (t *WxCardUserCard) SDelRecodeById(id int64) bool {
	item := new(WxCardUserCard)
	item.IfDel = 1
	_, err := lego.GetDBEngine().ID(id).Update(item)
	if err == nil {
		return true
	}
	return false
}

func (t *WxCardUserCard) SDelRecodeByWeappIdCardIdAndCardCode(weappid string, cardid string, cardcode string) bool {
	item := new(WxCardUserCard)
	item.IfDel = 1
	_, err := lego.GetDBEngine().Where("weapp_id = ?", weappid).
		And("card_id", cardid).
		And("user_card_code", cardcode).Update(item)
	if err == nil {
		return true
	}
	return false
}

func (t *WxCardUserCard) SDelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(WxCardUserCard)
	item.IfDel = 1
	_, err := session.ID(id).Update(item)
	if err == nil {
		return true
	}
	return false
}

/* 删除(硬删除)*/
func (t *WxCardUserCard) DelRecodeById(id int64) bool {
	item := new(WxCardUserCard)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *WxCardUserCard) DelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(WxCardUserCard)
	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 更新
 * -----------------*/
func (t *WxCardUserCard) UpdateRecode(rc *WxCardUserCard) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *WxCardUserCard) UpdateRecodeByWeappIdCardIdAndCardCode(rc *WxCardUserCard, weappid string, cardid string, cardcode string) bool {
	_, err := lego.GetDBEngine().Where("weapp_id = ?", weappid).
		And("card_id", cardid).
		And("user_card_code", cardcode).
		Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *WxCardUserCard) UpdateRecodeWithSession(session *xorm.Session, rc *WxCardUserCard) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}
